//
//  CYDetailCell.m
//  CreditAddressBook
//
//  Created by Lee on 15/5/11.
//  Copyright (c) 2015年 Lee. All rights reserved.
//

#import "CYDetailCell.h"
#import "CYDefaultRecordAO.h"
#import "CYCreditTool.h"
#import "CYDefaultRecordPics.h"

@interface CYDetailCell ()

@property (weak, nonatomic) IBOutlet UILabel *detailLabel;
@property (weak, nonatomic) IBOutlet UILabel *sourceLabel;
@property (weak, nonatomic) IBOutlet UIView *photoView;

@property(nonatomic, weak) UITableView *tableview;

@end

@implementation CYDetailCell

- (void)setDefrs:(NSArray *)defrs {
    _defrs = defrs;
    for (CYDefaultRecordAO *defr in defrs) {
         NSString *det = [NSString stringWithFormat:@"       %@于%@在**公司进行贷款为%@元，本应于%@之前进行还款，但至今仍未履行还款义务，逾期%@天，现对%@进行网络公示，望引起违约人的重视。\n", defr.name, [CYCreditTool timewithLongLongValue:[defr.lendTime longLongValue] formatter:nil], defr.amountPrice, [CYCreditTool timewithLongLongValue:[defr.startTime longLongValue] formatter:nil], [CYCreditTool dayWithStartValue:[NSNumber numberWithDouble:defr.startTime.doubleValue] endValue:[NSNumber numberWithDouble:defr.endTime.doubleValue]], defr.name];
            self.detailLabel.text = det;
        self.sourceLabel.text = CYStringFormat(@"*以上信息由%@", defr.typeName);
        self.defaultRecord = defr;
    }
}



- (void)loadPhoto:(UITableView *)tableview completion:(void(^)(CGFloat height))completion {

    if (!self.defaultRecord.recordPics) {
        CYWeakObject(wself, self);
        CYDefaultRecordAO *defr = self.defrs.firstObject;
        [CYCreditTool defaultPicsWithParams:@{@"defaultId": defr.defaultRecordId} success:^(CYDefaultRecordPics *defrs) {
            wself.defaultRecord.recordPics = defrs;
            UIView *photo = [defrs picAddressListViewCompletion:^(CGFloat viewH) {
                if (completion) {
                    completion(viewH);
                }
            }];
            [wself.photoView addSubview:photo];
            [photo autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero];
        } failure:^(NSError *error) {
            
        }];
    } else {
        UIView *photo = [self.defaultRecord.recordPics picAddressListViewCompletion:nil];
        [photo removeFromSuperview];
        [self.photoView addSubview:photo];
        [photo autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero];
    }
}

- (void)loaddetail:(UITableView *)tableview completion:(void(^)(CGFloat height))completion{
     if (!self.defaultRecord.recordPics) {
          CYWeakObject(wself, self);
          CYDefaultRecordAO *defr = self.defrs.firstObject;
          [CYCreditTool defaultPicsWithParams:@{@"defaultId": defr.defaultRecordId} success:^(CYDefaultRecordPics *defrs) {
               wself.defaultRecord.recordPics = defrs;
               UIView *photo = [defrs picAddressListViewCompletion:^(CGFloat viewH) {
                    if (completion) {
                         completion(viewH);
                    }
               }];
               [wself.photoView addSubview:photo];
               [photo autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero];
          } failure:^(NSError *error) {
               
          }];
     } else {
          UIView *photo = [self.defaultRecord.recordPics picAddressListViewCompletion:nil];
          [photo removeFromSuperview];
          [self.photoView addSubview:photo];
          [photo autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero];
     }

     
     
}

+ (instancetype)cellWithTableView:(UITableView *)tableView {
    static NSString *ID = @"newCell";
    CYDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"CYDetailCell" owner:nil options:nil] lastObject];
    }
    return cell;
}


- (void)awakeFromNib {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = [UIColor clearColor];
    self.userInteractionEnabled = NO;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

@end
